/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.turingtechnologies.materialscrollbardemo.adapters;

import com.turingtechnologies.materialscrollbardemo.ResourceTable;
import com.turingtechnologies.materialscrollbardemo.TitleInterFace;
import com.turingtechnologies.materialscrollbardemo.bean.IconBean;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class IconProvider extends BaseItemProvider implements TitleInterFace {
    private List<IconBean> sampleItemList;
    private Context context;

    /**
     * 构造函数
     *
     * @param aSampleItemList 数据集合
     * @param mContext 上下文
     */
    public IconProvider(List<IconBean> aSampleItemList, Context mContext) {
        this.sampleItemList = aSampleItemList;
        this.context = mContext;
    }

    @Override
    public int getCount() {
        return sampleItemList == null ? 0 : sampleItemList.size();
    }

    @Override
    public Object getItem(int position) {
        if (sampleItemList != null && position >= 0 && position < sampleItemList.size()) {
            return sampleItemList.get(position);
        }
        return "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_icon, null, false);
        } else {
            cpt = component;
        }

        Text text = (Text) cpt.findComponentById(ResourceTable.Id_item_index_icon);
        Image image = (Image) cpt.findComponentById(ResourceTable.Id_imageView);
        image.setPixelMap(sampleItemList.get(i).getImageId());
        text.setText(sampleItemList.get(i).getText());
        return cpt;
    }

    private String getCountry(int position) {
        return sampleItemList.get(position).getText();
    }

    @Override
    public String getSectionTitle(int position) {
        return getCountry(position);
    }
}
